package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.PmsBrandDao;
import com.atguigu.gulimall.product.entity.PmsBrandEntity;
import com.atguigu.gulimall.product.service.PmsCategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.service.PmsBrandService;
import org.springframework.util.StringUtils;


@Service("pmsBrandService")
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandDao, PmsBrandEntity> implements PmsBrandService {

    @Autowired
    PmsCategoryBrandRelationService pmsCategoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<PmsBrandEntity> page = this.page(
                new Query<PmsBrandEntity>().getPage(params),
                new QueryWrapper<PmsBrandEntity>()
                        .eq(!StringUtils.isEmpty(params.get("key")),"name",params.get("key"))
                        //TODO 前端多查
                        .eq(!StringUtils.isEmpty(params.get("brandId")),"brand_id",params.get("brandId"))
                        .like(!StringUtils.isEmpty(params.get("brandName")),"name",params.get("brandName"))
                        .like(!StringUtils.isEmpty(params.get("firstletter")),"first_letter",params.get("firstletter"))
        );

        return new PageUtils(page);

    }

    @Override
    public void updateDetail(PmsBrandEntity brand) {
        //保证冗余字段的数据一致
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName())){
            //同步更新其他关联表中的数据
            pmsCategoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            //TODO 更新其他关联
        }

    }

    @Override
    public List<PmsBrandEntity> getBrandsById(List<Long> brandIds) {
        return baseMapper.selectList(new QueryWrapper<PmsBrandEntity>().in("brand_id", brandIds));
    }

}